package com.d3works.apps.businessproject.service;

import com.d3works.apps.account.db.Invoice;
import com.d3works.apps.account.service.invoice.workflow.validate.WorkflowValidationServiceImpl;
import com.d3works.apps.businessproject.db.InvoicingProject;
import com.d3works.apps.businessproject.db.repo.InvoicingProjectRepository;
import com.d3works.exception.D3worksException;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

public class WorkflowValidationServiceProjectImpl extends WorkflowValidationServiceImpl {

  @Inject InvoicingProjectRepository invoicingProjectRepo;

  @Override
  @Transactional(rollbackOn = {Exception.class})
  public void afterValidation(Invoice invoice) throws D3worksException {
    super.afterValidation(invoice);

    InvoicingProject invoicingProject =
        invoicingProjectRepo.all().filter("self.invoice = ?", invoice.getId()).fetchOne();

    if (invoicingProject != null) {
      invoicingProject.setStatusSelect(InvoicingProjectRepository.STATUS_VALIDATED);
      invoicingProjectRepo.save(invoicingProject);
    }
  }
}
